<template>
	<view class="page" :style="'background-image: url('+indexBg+');'">
    <u-navbar :fixed="false" title="股东/合伙人" placeholder bgColor="transparent"></u-navbar>
    <view class="form-info">
      <view class="form-box u-m-b-48">
        <view class="form-item">
          <view class="form-lable required">股东类型</view>
          <view class="form-content-right">
            <u-radio-group v-model="form.investorType" placement="row" activeColor="#2b8e88">
              <u-radio
                :customStyle="{marginRight: '64rpx'}"
                label="个人"
                name="个人"
              />
              <u-radio
                label="企业"
                name="企业"
              />
            </u-radio-group>
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">股东/合伙人名称</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.investorName" />
          </view>
        </view>
        <view class="form-item" v-if="form.investorType == '企业'">
          <view class="form-lable required">统一社会信用代码</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.socialCode" />
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">联系人</view>
          <view class="form-content">
            <u--input placeholder="请输入" border="none" inputAlign="right" v-model="form.contact" />
          </view>
        </view>
        <view class="form-item">
          <view class="form-lable required">手机号码</view>
          <view class="form-content">
            <u--input tye="number" placeholder="请输入" :maxlength="11" border="none" inputAlign="right" v-model="form.phoneNumber" />
          </view>
        </view>
      </view>
      <view class="form-box u-m-b-32">
        <view class="form-item-top">
          <view class="form-lable">股东/合伙人简介</view>
          <view class="form-content">
            <u--textarea placeholder="请输入简介内容" border="none" v-model="form.investorDesc" />
          </view>
        </view>
      </view>
      <view class="form-box u-m-b-48">
        <view class="form-item">
          <view class="form-lable">状态</view>
          <view class="form-content-right">
            <view class="u-flex">
              <view class="u-m-r-16" style="color: #138E88;">{{ form.status }}</view>
              <u-switch v-model="form.status" activeColor="#2b8e88" activeValue="在册" inactiveValue="历史" size="20"></u-switch>
            </view>
          </view>
        </view>
      </view>
      <view class="save-primary-btn" @click="submit">保存提交</view>
    </view>
  </view>
</template>

<script>
import { getInvestor, addInvestor, updateInvestor } from '@/api/index'
import { phoneReg } from '@/common/util'

export default {
  data() {
    return {
      indexBg: 'https://gowork365.com/prod-api/profile/mini/index/indexBg1.png',
      id: '',
      form: {
        investorType: '',
        contact: '',
        phoneNumber: '',
        status: '在册',
      },
    }
  },
  watch: {
    'form.investorType'(n) {
      if(n == '企业') {
        this.form.socialCode = ''
      } else {
        delete this.form.socialCode
      }
    }
  },
  onLoad(options) {
    if(options.id) {
      this.id = options.id
      this.getForm()
    }
  },
  methods: {
    getForm() {
      uni.showLoading({ mask: true })
      getInvestor(this.id).then(response => {
        uni.hideLoading()
        this.form = response.data;
      });
    },
    submit() {
      const that = this
      let formFlag = false
      for(let key in this.form) {
        if(this.form[key] === '') {
          formFlag = true
        }
      }
      if(formFlag) {
        uni.showToast({
          icon: 'none',
          title: '请完善基本信息'
        })
        return
      }
      if(!phoneReg(this.form.phoneNumber)) {
        return
      }
      uni.showLoading({ mask: true })
      if (this.form.id != null) {
        updateInvestor(this.form).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '修改成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      } else {
        addInvestor(this.form).then(response => {
          uni.hideLoading()
          uni.showToast({
            icon: 'none',
            title: '新增成功'
          })
          setTimeout(() => {
            that.goBack(1)
          }, 1000)
        });
      }
    }
  }
}
</script>

<style lang="less" scoped>
.page {
  width: 100%;
  min-height: 100vh;
  background: #F4F7F6;
  position: relative;
  background-repeat: no-repeat;
  background-size: 750rpx 690rpx;
  padding-bottom: 188rpx;
  display: flex;
  flex-direction: column;
  .form-info {
    flex: 1;
  }
}
</style>
